package com.emapp.lccode.adapter

import android.util.SparseArray
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.emapp.lccode.base.BaseApplication
import com.emapp.lccode.fragment.HomeKTFragment
import com.emapp.lccode.fragment.MapKtFragment
import com.emapp.lccode.fragment.MeKtFragment

class MyViewPgerKtAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {

    companion object {
        const val PAGE_HOME = 0
        const val PAGE_CHECK = 1
        const val PAGE_USER = 2
    }

    private val fragments: SparseArray<Fragment> = SparseArray()

    init {
        val user= BaseApplication.instance.getUser()
        if (user?.isChecker() == true){
            fragments.put(PAGE_HOME, HomeKTFragment())
            fragments.put(PAGE_CHECK, MapKtFragment())
            fragments.put(PAGE_USER, MeKtFragment())
        }else{
            fragments.put(PAGE_HOME, HomeKTFragment())
            fragments.put(PAGE_CHECK, MeKtFragment())
        }
    }

    override fun createFragment(position: Int): Fragment {
        return fragments[position]
    }

    override fun getItemCount(): Int {
        return fragments.size()
    }
}